﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace map.manage
{
    public partial class configshop_shopcata : System.Web.UI.Page
    {
        string ParentID = "0";
        DataTable c;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
            }
        }
        private void BindTree()
        {
            //string id = AliApi.appUserId;
            //if (Request.Params["id"] != null)
            //{
            //    cbuser u = cbuser.Find(" recid =  " + Request.Params["id"].ToString());
            //    if (u.Count > 0)
            //    {
            //        id = u.cbuserid.ToString();
            //    }
            //}
            //if (id == "")
            //{
            //    //return ""; //为了方便测试,暂时注销
            //}
            cbuser u = cbuser.getcuruser();
            string id = u.cbuserid.ToString();
            cbshopcss sc = cbshopcss.findorcreate(id);
            string css = sc.getstylestring();
            Literalcss.Text = css;


            string sql = " select  taobaocid, taobaoparentcid, taobaoname from taobaoItemCat ";
            sql += " where taobaouserid = '" + id + "'";
            c = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];


            TreeView1.Nodes.Clear();
            BindTreeNode(TreeView1.Nodes, ParentID);
            TreeView1.ExpandAll();
        }
        private int BindTreeNode(TreeNodeCollection nodeCollection, string parentNodeId)
        {
            string filter = " taobaoparentcid='" + parentNodeId
                + "'";
            DataRow[] dRows = c.Select(filter);
            int count = 0;

            foreach (DataRow dRow in dRows)
            {
                count++;
                TreeNode childNode;
                childNode = new TreeNode();
                childNode.Value = dRow["taobaocid"].ToString();
                childNode.Text =  dRow["taobaoname"].ToString();
                //  +"   <img src=\"../img/cbt.gif\" />";

                int childCount = BindTreeNode(childNode.ChildNodes, dRow["taobaocid"].ToString());
                if (childCount > 0)
                    nodeCollection.AddAt(0, childNode);
                else
                    nodeCollection.Add(childNode);
            }
            return count;
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            if ( this.TreeView1.SelectedNode != null)
            {
                string id = AliApi.appUserId;
                if (Request.Params["id"] != null)
                {
                    cbuser u = cbuser.Find(" recid =  " + Request.Params["id"].ToString());
                    if (u.Count > 0)
                    {
                        id = u.cbuserid.ToString();
                    }
                }
                //cbcata ct = cbcata.Find(" cbcataid = '" + this.TreeView1.SelectedNode.Value + "'");
                cbuser u2 = cbuser.Find("cbuserid = '"+id+"'");
                string url = ResolveUrl("~/member/default.aspx?cbshoppageid=1&id="
                    + u2.RECID.ToString() + "&itemcataid=" + this.TreeView1.SelectedNode.Value
                    );
                ScriptManager.RegisterStartupScript(this.TreeView1, this.TreeView1.GetType(), "hasSelected",
                        "<script type =\"text/javascript\" >redirect('" + url + "');</script>", false);


            }
        }
    }
}
